package io.renren.modules.seller.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.modules.seller.entity.ItemCatEntity;
import io.renren.modules.seller.entity.PageResult;
import io.renren.modules.seller.entity.Result;
import io.renren.modules.seller.service.ItemCatService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 商品类目
 *
 * @author Mark 15373178086@163.com
 * @date 2020-02-16 11:30:55
 */
@Slf4j
@RestController
@RequestMapping("manage/itemCat")
public class ItemCatController {

    @Autowired
    private ItemCatService itemCatService;

    /**
     * 返回全部列表
     *
     * @return ItemCatEntity
     */
    @RequestMapping("/findAll")
    public List<ItemCatEntity> findAll() {
        return itemCatService.list();
    }

    /**
     * 返回全部列表
     *
     * @return PageResult
     */
    @RequestMapping("/findPage")
    public PageResult findPage(Map<String, Object> params) {
        log.info("item cat findPage params:{}", params);
        return itemCatService.findPage(params);
    }

    /**
     * 查询+分页
     *
     * @return PageResult
     */
    @RequestMapping("/search")
    public PageResult search(Map<String, Object> params) {
        log.info("item cat search params:{}", params);
        return itemCatService.findPage(params);
    }

    /**
     * 增加
     *
     * @return Result
     */
    @RequestMapping("/add")
    public Result add(@RequestBody ItemCatEntity itemCat) {
        try {
            itemCatService.save(itemCat);
            return new Result(true, "增加成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Result(false, "增加失败");
        }
    }

    /**
     * 修改
     *
     * @return Result
     */
    @RequestMapping("/update")
    public Result update(@RequestBody ItemCatEntity itemCat) {
        try {
            itemCatService.updateById(itemCat);
            return new Result(true, "修改成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return new Result(false, "修改失败");
        }
    }

    /**
     * 获取实体
     *
     * @return ItemCatEntity
     */
    @RequestMapping("/findOne")
    public ItemCatEntity findOne(Long id) {
        return itemCatService.getById(id);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return Result
     */
    @RequestMapping("/delete")
    public Result delete(Long[] ids) {
        try {
            itemCatService.removeByIds(Arrays.asList(ids));
            return new Result(true, "删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.fail("删除失败");
        }
    }

    /**
     * 根据上级ID查询商品分类列表
     *
     * @return ItemCatEntitys
     */
    @RequestMapping("/findByParentId")
    public List<ItemCatEntity> findByParentId(Long parentId) {
        Wrapper<ItemCatEntity> wrapper = new QueryWrapper<ItemCatEntity>().eq("parent_id", parentId);
        return itemCatService.list(wrapper);
    }

}
